package com.example.boardroom.demo;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.boardroom.R;

/**
 * 版权：易金 版权所有
 * <p>
 * 作者：suntinghui
 * <p>
 * 创建日期：2020/3/3 0003 15:03
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class WebViewActivity extends BaseActivity implements View.OnClickListener {

    ImageView ivLeftCommon;
    TextView tvTitleCommon;
    LinearLayout ll;
    String url = "http://192.168.1.150:3000/project/48/interface/api";
//    String url = "http://192.168.1.168:3000/";
    //http://192.168.1.150:3000/project/48/interface/api
    private WebView webView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        webView=findViewById(R.id.lay_webview);
        initView();
   }

    protected void initView() {

//        webView = new WebView(this);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setSavePassword(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setTextZoom(100);
        webSettings.setSupportZoom(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        xxx();

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
//                tvTitleCommon.setText(view.getTitle());
            }
        });
        webView.loadUrl(url);
    }


    @Override
    protected void onDestroy() {
        webView.destroy();
        webView = null;
        super.onDestroy();
    }

    private void xxx(){
        WebSettings webSettings = webView.getSettings();



        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webSettings.setUseWideViewPort(true);//关键点

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        webSettings.setDisplayZoomControls(true);

        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本

        webSettings.setAllowFileAccess(true); // 允许访问文件

        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮

        webSettings.setSupportZoom(true); // 支持缩放

        webSettings.setLoadWithOverviewMode(true);

        DisplayMetrics metrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int mDensity = metrics.densityDpi;

//        Log.d("maomao”, "+densityDpi ="+ mDensity);

        if (mDensity == 240) {

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

        } else if (mDensity == 160) {

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);

        } else if(mDensity == 120) {

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);

        }else if(mDensity == DisplayMetrics.DENSITY_XHIGH){

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

        }else if (mDensity == DisplayMetrics.DENSITY_TV){

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);

        }else{

            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);

        }
    }
    
    @Override
    public void onClick(View v) {
        finish();
    }
}
